package com.aishare.qicaitaoke.utils;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;


import com.aishare.qicaitaoke.IApp;

import java.util.Stack;


public enum ActivityUtil {
    INSTANCE;

    private IApp application;//基础的Application,必须设置
    public final Stack<Activity> activityStack = new Stack<>();

    /**
     * key=activity.getClass().getName()
     * value= activity
     */
    public void add(Activity activity) {
        synchronized (activityStack) {
            activityStack.add(activity);
        }
    }

    public Activity getTop() {
        Activity activity = null;
        synchronized (activityStack) {
            if (!activityStack.isEmpty()) {
                activity = activityStack.peek();
            }
        }
        return activity;
    }

    /**
     * 结束最新加入的Activity
     */
    public void finish() {
        Activity activity = null;
        synchronized (activityStack) {
            if (!activityStack.isEmpty()) {
                activity = activityStack.pop();
            }
        }
        finish(activity);
    }
    /**
     * 结束所有activity
     */
    public void finishAll(){
        for(Activity activity:activityStack){
            finish(activity);
        }
    }

    public void finishALlExcept(Class clazz){
        synchronized (activityStack){
            for(Activity activity:activityStack){
                if(activity.getClass()!=clazz){
                    activityStack.remove(activity);
                    activity.finish();
                }
            }
        }
    }

    /**
     * 移除最新加入的Activity
     */
    public void removeTop() {
        synchronized (activityStack) {
            if (!activityStack.isEmpty()) {
                activityStack.pop();
            }
        }
    }

    public void finish(Activity ac) {
        if (ac != null) {
            ac.finish();
        }
    }
    public void finish(Class clazz){
        synchronized (activityStack) {
            for(Activity activity:activityStack){
                    if(activity.getClass()==clazz){
                        activityStack.remove(activity);
                        finish(activity);
                        break;
                    }
            }
        }

    }

    /**
     * 仅从actviityMap中移除Activity对象
     *
     * @param activity
     */
    public void remove(Activity activity) {
        synchronized (activityStack) {
            if (!activityStack.isEmpty()) {
                activityStack.remove(activity);
            }
        }
    }


    public IApp getApplication() {
        return application;
    }

    public void setApplication(IApp application) {
        this.application = application;
    }

    /**
     * 使用application即可跳转,跳转只需包名即可
     *
     * @param cls
     */
    public void jump(Class cls) {
        if (application == null) {
            Log.e("application","application is null!");
            return;
        }

        Intent intent = new Intent(application, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        application.startActivity(intent);
    }
}
